package com.crk.yiyuanguahao.service.impl;

import com.crk.yiyuanguahao.dao.DoctorDao;
import com.crk.yiyuanguahao.dao.DoctorReplayDao;
import com.crk.yiyuanguahao.dao.UserDao;
import com.crk.yiyuanguahao.dao.UserRemarkDao;
import com.crk.yiyuanguahao.entity.*;
import com.crk.yiyuanguahao.service.UserRemarkService;
import com.crk.yiyuanguahao.util.Page;
import com.crk.yiyuanguahao.util.ResponseResult;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author
 * @description:
 * @date 2020/3/26
 */
@Service
public class UserRemarkServiceImpl implements UserRemarkService {
    @Autowired
    UserRemarkDao userRemarkDao;
    @Autowired
    UserDao userDao;
    @Autowired
    DoctorReplayDao doctorReplayDao;
    @Autowired
    DoctorDao doctorDao;


    /**
     * 保存用户留言
     *
     * @param userRemark 用户留言
     * @return
     */
    @Override
    public ResponseResult saveUserRemark(UserRemark userRemark) {
        if (null != userRemark){
            int result = 0;
            if (userRemark.getId() == null){
                userRemark.setStatus(1);
                userRemark.setCreateTime(new Date());
                User userInfo = (User) SecurityUtils.getSubject().getPrincipal();
                userRemark.setUserId(userInfo.getId());
                result = userRemarkDao.insert(userRemark);
            }else{
                result = userRemarkDao.updateByPrimaryKey(userRemark);
            }

            if (result > 0){
                return ResponseResult.success(userRemark);
            }else{
                return ResponseResult.fail();
            }
        }else {
            return ResponseResult.fail();
        }
    }

    /**
     * 删除用户留言
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteUserRemark(Integer id) {
        int i = userRemarkDao.deleteByPrimaryKey(id);
        if(i>0){
            return ResponseResult.success();
        }else{
            return ResponseResult.fail();
        }
    }

    /**
     * 查询用户留言
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult getUserRemark(Integer id) {
        UserRemark userRemark = userRemarkDao.selectByPrimaryKey(id);
        UserRemarkDto userRemarkDto = new UserRemarkDto();
        User sysUserById = userDao.getSysUserById(userRemark.getUserId());
        userRemarkDto.setUserName(sysUserById.getUserName());
        userRemarkDto.setContent(userRemark.getContent());
        userRemarkDto.setCreateTime(userRemark.getCreateTime());
        userRemarkDto.setStatus(userRemark.getStatus());
        userRemarkDto.setId(userRemark.getId());
        return ResponseResult.success(userRemarkDto);
    }

    /**
     * 查询用户留言列表
     *
     * @param status 审核状态
     * @param pageInfo   分页
     * @return
     */
    @Override
    public ResponseResult queryUserRemarkPage(int status, Page pageInfo) {
        Integer count = userRemarkDao.countByCondition(status);
        pageInfo.setAllRow(count);
        List<UserRemark> doctorList = userRemarkDao.selectByCondition(status, pageInfo.getStart(), pageInfo.getSize());
        List<UserRemarkDto> userRemarkDtos = new ArrayList<>();
        for (UserRemark userRemark : doctorList) {
            UserRemarkDto userRemarkDto = new UserRemarkDto();
            User sysUserById = userDao.getSysUserById(userRemark.getUserId());
            userRemarkDto.setUserName(sysUserById.getUserName());
            userRemarkDto.setContent(userRemark.getContent());
            userRemarkDto.setCreateTime(userRemark.getCreateTime());
            userRemarkDto.setStatus(userRemark.getStatus());
            userRemarkDto.setId(userRemark.getId());
            userRemarkDtos.add(userRemarkDto);
        }
        return ResponseResult.success(userRemarkDtos);
    }

    /**
     * 审批
     *
     * @param id
     * @param status
     * @return
     */
    @Override
    public ResponseResult approval(Integer id, Integer status) {
        int i = userRemarkDao.updateStatus(status,id);
        if (i>0){
            return ResponseResult.success();
        }else{
            return ResponseResult.fail();
        }
    }

    @Override
    public ResponseResult queryLastUserRemark() {
        List<UserRemark> userRemarks = userRemarkDao.queryLastUserRemark();
        List<UserRemarkDto> userRemarkDtos = new ArrayList<>();
        for (UserRemark userRemark : userRemarks) {
            UserRemarkDto userRemarkDto = new UserRemarkDto();
            User sysUserById = userDao.getSysUserById(userRemark.getUserId());
            userRemarkDto.setUserName(sysUserById.getUserName());
            userRemarkDto.setContent(userRemark.getContent());
            userRemarkDto.setCreateTime(userRemark.getCreateTime());
            userRemarkDto.setStatus(userRemark.getStatus());
            userRemarkDto.setId(userRemark.getId());
            userRemarkDtos.add(userRemarkDto);
        }
        return ResponseResult.success(userRemarkDtos);
    }

    @Override
    public ResponseResult getDetailInfo(Integer id) {
        UserRemark userRemark = userRemarkDao.selectByPrimaryKey(id);
        List<DoctorReplay> doctorReplay = doctorReplayDao.getDoctorReplay(id);
        UserRemarkDto userRemarkDto = new UserRemarkDto();
        List<DoctorReplayDto> doctorReplayDtos = new ArrayList<>();
        for (DoctorReplay replay : doctorReplay) {
            DoctorReplayDto doctorReplayDto = new DoctorReplayDto();
            Doctor doctor = doctorDao.selectDoctorByCode(replay.getDoctorCode());
            doctorReplayDto.setDoctorName(doctor.getDoctorName());
            doctorReplayDto.setReplay(replay.getReplay());
            doctorReplayDto.setCreateTime(replay.getCreateTime());
            doctorReplayDtos.add(doctorReplayDto);
        }
        userRemarkDto.setId(userRemark.getId());
        userRemarkDto.setContent(userRemark.getContent());
        userRemarkDto.setDoctorReplayDtoList(doctorReplayDtos);
        return ResponseResult.success(userRemarkDto);
    }

    @Override
    public ResponseResult saveReplay(Integer id, String replay) {
        DoctorReplay doctorReplay = new DoctorReplay();
        doctorReplay.setRemarkId(id);
        doctorReplay.setReplay(replay);
        doctorReplay.setCreateTime(new Date());
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        doctorReplay.setDoctorCode(user.getDoctorCode());
        int insert = doctorReplayDao.insert(doctorReplay);
        if (insert > 0){
            return ResponseResult.success();
        }else {
            return ResponseResult.fail();
        }
    }
}
